package funcional.reserva;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import util.ErroAcessoRepositorioException;
import util.Serializador;
import funcional.reserva.exception.ReservaInvalidaException;
import funcional.reserva.exception.SuiteJaReservadaException;
 

public class RepositorioReservasSuitesFile implements IRepositorioReservaSuites {

	Serializador serializador;
	List<ReservaSuite> reservas;
	
	protected static final String MSG_ERRO_ACESSO_BD =
		"Erro de acesso ao banco de dados";
	
	public RepositorioReservasSuitesFile() {
		try {
			serializador = new Serializador();
			reservas  = lerReservas();
			//
		} catch (IOException e) {
			reservas = new ArrayList<ReservaSuite>();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public void atualizarReserva(ReservaSuite reserva) throws ReservaInvalidaException, ErroAcessoRepositorioException{
		Iterator<ReservaSuite> i = reservas.iterator();
		while(i.hasNext()){
			ReservaSuite reservaAux = i.next();
			if(reserva.getSuite().getNumeroSuite()==reservaAux.getSuite().getNumeroSuite()){
				reservas.remove(reservaAux);
				reservas.add(reserva);
				try{
					serializador.escreverObjeto(reservas);
					break;
				}catch(IOException ioe){
					reservas.add(reservaAux);
					reservas.remove(reserva);
					throw new ErroAcessoRepositorioException(MSG_ERRO_ACESSO_BD,
							ioe,
							1);
				}
			}
		}
	}

	public ReservaSuite consultarReserva(int codigoSuite) throws ReservaInvalidaException{
		ReservaSuite resp = null;
		Iterator<ReservaSuite> i = reservas.iterator();
		while(i.hasNext()){
			ReservaSuite reservaAux = i.next();
			if(codigoSuite==reservaAux.getSuite().getNumeroSuite()){
				resp = reservaAux; 
				break;
			}
		}
		if (resp==null){
			throw new ReservaInvalidaException(codigoSuite);
		}
		return resp;
	}

	public void excluirReserva(int codigoSuite) throws ReservaInvalidaException, ErroAcessoRepositorioException {
		ReservaSuite resp = null;
		Iterator<ReservaSuite> i = reservas.iterator();
		while(i.hasNext()){
			ReservaSuite reservaAux = i.next();
			if(codigoSuite==reservaAux.getSuite().getNumeroSuite()){
				resp = reservaAux;
				reservas.remove(reservaAux);
				try{
					serializador.escreverObjeto(reservas);
					break;	
				}catch(IOException ioe){
					reservas.add(reservaAux);
					throw new ErroAcessoRepositorioException(MSG_ERRO_ACESSO_BD,
							ioe,
							1);
				}
			}
		}
		if (resp==null){
			throw new ReservaInvalidaException(codigoSuite);
		}
	}
	
	public List<ReservaSuite> listarReservas() {
		return reservas;
	}

	private ArrayList<ReservaSuite> lerReservas() throws IOException, ClassNotFoundException {
		 ArrayList<ReservaSuite> resp = (ArrayList<ReservaSuite>) serializador.lerObjeto();
		 if (resp == null){
			 resp = new ArrayList<ReservaSuite>();
		 }
		 return resp;
	}
	
	public void inserirReserva(ReservaSuite reserva) throws SuiteJaReservadaException, ErroAcessoRepositorioException {
		existeReserva(reserva);
		reservas.add(reserva);
		try {
			serializador.escreverObjeto(reservas);
		} catch (IOException ioe) {
			throw new ErroAcessoRepositorioException(MSG_ERRO_ACESSO_BD,
					ioe,
					1);
		} 
	}

	private void existeReserva(ReservaSuite reserva) throws SuiteJaReservadaException {
		Iterator<ReservaSuite> i = reservas.iterator();
		while(i.hasNext()){
			ReservaSuite reservaAux = i.next();
			if(reserva.getSuite().getNumeroSuite()==reservaAux.getSuite().getNumeroSuite()){
				throw new SuiteJaReservadaException(reserva.getSuite().getNumeroSuite());
			}
		}
	}
	

	
}
